package com.qf.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.qf.clients.TeacherClients;
import com.qf.config.RedisKey;
import com.qf.mapper.CourseMapper;
import com.qf.pojo.respVo.BaseResp;
import com.qf.pojo.vo.entity.QfCourse;
import com.qf.pojo.vo.entity.QfTeach;
import com.qf.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 陌人不故
 * @Date: 2022/10/26/21:47
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    CourseMapper courseMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Resource
    TeacherClients teacherClients;

    @Override
    public BaseResp findCourseByClassify(Integer id,Integer page,Integer size) {
        //判断redis中是否存在
        List list = redisTemplate.boundListOps(RedisKey.COURSE_LIST + id.toString()).range(0,-1);
        if (list == null || list.size() == 0){
            //没有，建立缓存
            List<QfCourse> courseByClassify = courseMapper.findCourseByClassify(id);
            if (courseByClassify == null || courseByClassify.size() == 0){
                redisTemplate.opsForList().rightPush(RedisKey.COURSE_LIST + id.toString(),"没有该分类的数据!");
                //数据库没有该id对应的数据，设置一个值，并设置一个30s的过期时间，防止缓存穿透
                redisTemplate.expire(RedisKey.COURSE_LIST + id.toString(),30, TimeUnit.SECONDS);
            }
            //创建对应的缓存
            redisTemplate.opsForList().rightPushAll(RedisKey.COURSE_LIST + id.toString(),courseByClassify);
        }
        //分页
        Integer start =(page-1)*size;
        //分页查询到的数据
        List list1 = redisTemplate.boundListOps(RedisKey.COURSE_LIST + id.toString()).range(start, start + size - 1);
        //获得总条数
        Long total = redisTemplate.boundListOps(RedisKey.COURSE_LIST + id.toString()).size();
        return BaseResp.SUCCESS("查询成功!",list1,total);
    }

    /**
     *根据id查询课程详情
     * @param id
     * @return
     */
    @Override
    public BaseResp findCourseDetailById(Integer id) {
        Boolean aBoolean = redisTemplate.boundHashOps(RedisKey.COURSE_LIST).hasKey(id.toString());
        if (!aBoolean){
            QfCourse qfCourse = courseMapper.selectById(id);
            BaseResp teachResp = teacherClients.findById(qfCourse.getCourseTeachs());
            // System.out.println("查询教师==》"+teachResp);
            if (teachResp.getCode() == 1){
                Object data = teachResp.getData();
                QfTeach qfTeach = JSONObject.parseObject(JSON.toJSONString(data), QfTeach.class);
                // System.out.println("远程教师=="+qfCourse);
                qfCourse.setQfTeach(qfTeach);
            }
            redisTemplate.boundHashOps(RedisKey.COURSE_LIST).put(id.toString(),qfCourse);
        }
        Object o = redisTemplate.boundHashOps(RedisKey.COURSE_LIST).get(id.toString());
        return BaseResp.SUCCESS("查询成功!",o,null);
    }
}
